﻿using System;
using System.Linq;
using Telerik.Web.UI;
using Thuoc24h.Business;
using Thuoc24h.Common;
using Thuoc24h.Data;
namespace Thuoc24h.AdminCP.Pages
{
    public partial class SupportEdit : BasePage.BasePages
    {
        private int _id;
        private SupportChat _obj = new SupportChat();

        protected void Page_Load(object sender, EventArgs e)
        {
            _id = Convert.ToInt32(Request.QueryString["ID"]);
            if (!Page.IsPostBack)
            {

                if (_id > 0)
                {
                    btnSave.Text = ActionNames.Update;
                    BindData();
                }
                else
                {
                    btnSave.Text = ActionNames.AddNew;
                }

                BindDataToGrid();
            }

        }

        private void GetData()
        {
            _obj.Data = txtName.Text;
            _obj.PhoneNumber = "";
            _obj.FullName = "";
            _obj.Type = Convert.ToInt32(ccSupportType.SelectedValue);

        }
        private void Reset()
        {
            ccSupportType.SelectedIndex = 0;

           // txtFullname.Text = txtPhoneNumber.Text = txtName.Text = string.Empty;

        }
        private void SaveData()
        {
            if (_id > 0)
            {
                _obj = context.SupportChats.SingleOrDefault(item => item.Id == _id);
                if (_obj != null)
                {
                    GetData();
                }
            }
            else
            {
                GetData();
                context.SupportChats.AddObject(_obj);
            }

            if (context.SaveChanges() > 0)
            {
                Reset();
                Response.Redirect(UrlManager.Admin.SupportOnlineEdit);
            }
        }
        private void BindData()
        {

            _obj = ReviewRepository.SupportById(_id);
            txtName.Text = _obj.Data;
            //txtFullname.Text = _obj.FullName;
            //txtPhoneNumber.Text = _obj.PhoneNumber;
            ccSupportType.SelectedValue = _obj.Type.ToString();
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            SaveData();

        }

        protected void OnClick(object sender, EventArgs e)
        {
            Reset();
        }

        private void BindDataToGrid()
        {
            gridData.DataSource = ReviewRepository.GetAllSupports();
            gridData.DataBind();
        }

        protected void gridData_ItemCommand(object sender, GridCommandEventArgs e)
        {
            string id = string.Empty;

            var commandName = e.CommandName;
            var gridDataItem = e.Item as GridDataItem;
            if (gridDataItem != null)
            {
                id = gridDataItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString();
            }

            switch (commandName)
            {

                case CommandNames.Delete:
                    ReviewRepository.SupportDelete(id.ToInt32Return0());
                    BindDataToGrid();
                    break;
                case CommandNames.Edit:
                    Utils.Redirect(UrlManager.Admin.SupportOnlineEdit, id);
                    break;
            }
        }
        public static string GetType(object typeId)
        {
            try
            {
                int type = int.Parse(typeId.ToString());
                switch (type)
                {
                    case 1:
                        return "Skype";
                    case 2:
                        return "Yahoo";
                    case 3:
                        return "Điện thoại";
                    default:
                        return "";
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}